﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class search_report : System.Web.UI.Page
{
	public string EnterpriseName = "";
    public string UserID = "";
    public string UserPower = "";
    public string Footer = "";
	
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Init.islogin(ref UserID,ref EnterpriseName,ref UserPower))
        {
            Response.Redirect("/Login.aspx");
        }
        this.EnableViewState = false;
        Response.Write(Init.InitIndex(EnterpriseName, UserPower));
        Footer = Init.IndexFooter();
        djbh = Request.QueryString["d"];
        pihao = Request.QueryString["n"];
        keyword = Request.QueryString["k"];
        if (!string.IsNullOrEmpty(djbh) || !string.IsNullOrEmpty(pihao) || !string.IsNullOrEmpty(keyword))
        {
            Literal1.Text = DataList(djbh, pihao, keyword);
        }        
    }
    Common Init = new Common();
    MsSqlHelper mssql = new MsSqlHelper();
    public string djbh = "";
    public string pihao = "";
    public string keyword = "";
    public DataSet DataGroup(string djbh="",string pihao="",string keyword="")
    {
        string sql = "";
        if (string.IsNullOrEmpty(EnterpriseName)||UserPower.Contains("Manager"))
        {
            sql += "select distinct ";
            sql += (!string.IsNullOrEmpty(djbh)) ? "cast(case when (select count(imageid) from [ImageDoc](nolock) where beactive='是' and detailid=rtrim(b.spid) and replace(rtrim(fzxx1),' ','')=replace(rtrim(a.pihao),' ',''))>0 then rtrim(b.spid)+'|'+replace(rtrim(a.pihao),' ','') else '' end as varchar(50)) as plh," : "rtrim(spid)+'|'+replace(rtrim(fzxx1),' ','') as plh,";
            sql += (!string.IsNullOrEmpty(djbh)) ? " rtrim(a.spid) as plhup,a.remind_bg,rtrim(a.djbh) as djbh,rtrim(a.pihao) as pihao," : "rtrim(b.spid) as plhup,'' as remind_bg,rtrim(b.spid) as djbh,rtrim(a.fzxx1) as pihao,";
            sql += " rtrim(b.spid) as spid,rtrim(b.spbh) as spbh,rtrim(b.spmch) as spmch,rtrim(b.shpgg) as shpgg,rtrim(b.dw) as dw,rtrim(b.shengccj) as shengccj,rtrim(b.pizhwh) as pizhwh,'' as url ";
            sql += (!string.IsNullOrEmpty(djbh)) ? " from spkfk b(nolock) left join spls_ck a(nolock) on a.spid=b.spid " : " from  [spkfk] b(nolock) left join [ImageDoc] a(nolock) on a.beactive='是' and a.detailid=b.spid ";
            sql += " where 1=1 ";
            sql += (!string.IsNullOrEmpty(djbh)) ? " and a.djbh='" + djbh + "' " : "";
            sql += (!string.IsNullOrEmpty(pihao) && !string.IsNullOrEmpty(djbh)) ? " and a.pihao like '" + pihao + "%' " : "";
            sql += (!string.IsNullOrEmpty(pihao) && string.IsNullOrEmpty(djbh)) ? " and a.fzxx1 like '" + pihao + "%' " : "";
            sql += (!string.IsNullOrEmpty(keyword)) ? " and (b.spbh like '%" + keyword + "%' or b.spmch like '%" + keyword + "%' or b.zjm like '%" + keyword + "%' or b.pizhwh like '%" + keyword + "%')" : "";
            sql += (string.IsNullOrEmpty(djbh)) ? "group by spid,spbh,spmch,shpgg,dw,shengccj,pizhwh,fzxx1" : "";
            sql += " order by rtrim(b.spid) desc";
        }
        else
        {
            sql += "select distinct ";
            sql += "cast(case when (select count(imageid) from [ImageDoc](nolock) where beactive='是' and detailid=rtrim(b.spid) and replace(rtrim(fzxx1),' ','')=replace(rtrim(a.pihao),' ',''))>0 then rtrim(b.spid)+'|'+replace(rtrim(a.pihao),' ','') else '' end as varchar(50)) as plh,";
            sql += " rtrim(a.spid) as plhup,a.remind_bg,"
                //+"rtrim(a.djbh) as djbh,"
                +"rtrim(a.pihao) as pihao," ;
            sql += " rtrim(b.spid) as spid,rtrim(b.spbh) as spbh,rtrim(b.spmch) as spmch,rtrim(b.shpgg) as shpgg,rtrim(b.dw) as dw,rtrim(b.shengccj) as shengccj,rtrim(b.pizhwh) as pizhwh,'' as url ";
            sql += " from [spkfk] b(nolock) left join [spls_ck] a(nolock) on a.spid=b.spid ";

            sql += " where a.dwbh in(select dwbh from [mchk](nolock) where dwmch='"+EnterpriseName+"') ";
            sql += (!string.IsNullOrEmpty(djbh)) ? " and a.djbh='" + djbh + "' " : "";
            sql += (!string.IsNullOrEmpty(pihao)) ? " and a.pihao like '" + pihao + "%' " : "";            
            sql += (!string.IsNullOrEmpty(keyword)) ? " and (b.spbh like '%" + keyword + "%' or b.spmch like '%" + keyword + "%' or b.zjm like '%" + keyword + "%' or b.pizhwh like '%" + keyword + "%')" : "";
           // sql += (string.IsNullOrEmpty(djbh)) ? "group by spid,spbh,spmch,shpgg,dw,shengccj,pizhwh,fzxx1" : "";
            sql += " order by rtrim(b.spid) desc";
        }
        if (Init.IsDebug)
        {
            ResultDeBug.Text = sql;
        }
       // DataSet dss = mssql.ExecuteDs("select top 3 spid,spid as plh,spbh,spmch,dw,shpgg,shengccj,'' as pihao,'' as url from spkfk");
        //Init.WriteLog(sql,false);
        DataSet dss = mssql.ExecuteDs(sql);
        for (int i = 0; i < dss.Tables[0].Rows.Count; i++)
        {
            dss.Tables[0].Rows[i]["url"] = herf(dss.Tables[0].Rows[i]["spid"].ToString(), dss.Tables[0].Rows[i]["pihao"].ToString(), dss.Tables[0].Rows[i]["plhup"].ToString(),dss.Tables[0].Rows[i]["remind_bg"].ToString());
        }         
        return dss;            
    }
    public string DataList(string djbh = "", string pihao = "", string keyword = "") 
    {
        string result = "";
        result+="<table border=\"1\" style=\"border:1px solid #ccc;width:100%;\">\r\n"
			    +"<tbody>\r\n"
			    +"<tr height=\"50\" style=\"background: #eee;text-align: center\">\r\n"
				+"<td width=\"60\" style=\"text-align: left;padding-left:10px;\"><label class=\"searxh-content-label\"><input class=\"check_all\" type=\"checkbox\">全选</label></td>\r\n"
				+"<td>编号</td><td>名称</td><td width=\"77\">规格</td><td width=\"78\">单位</td><td>生产厂家</td><td width=\"96\">批次号</td><td>操作</td>\r\n"
				+"</tr>";

        DataSet ds = DataGroup(djbh, pihao, keyword);
        DataTable dt = ds.Tables[0];
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            result += "<tr height=\"50\" style=\"text-align: center\">\r\n"
                    + "<td><label class=\"searxh-content-label\"><input type=\"checkbox\" ";
            result += dt.Rows[i]["plh"].ToString().Trim().ToLower() == "" ? "disabled=\"disabled\"" : "name=\"ids\" value=\"" + dt.Rows[i]["plh"];
            result += "\" />"+(i+1).ToString()+"</label></td>\r\n"
					+"<td>"+dt.Rows[i]["spbh"]+"</td>\r\n"
                    +"<td class=\"drug_name\">"+dt.Rows[i]["spmch"]+"</td>\r\n"
					+"<td class=\"drug_point\">"+dt.Rows[i]["dw"]+"</td>\r\n"
				    
                    +"<td class=\"drug_rule\">"+dt.Rows[i]["shpgg"]+"</td>\r\n"
					+"<td class=\"drug_changjia\">"+dt.Rows[i]["shengccj"]+"</td>\r\n"
                    + "<td class=\"drug_batch_number\">" + dt.Rows[i]["pihao"] + "</td>\r\n"
					+"<td>"+dt.Rows[i]["url"]+"</td>\r\n"
					+"</tr>\r\n";
        }
        result += "<tr height=\"50\">\r\n"
				 +"<td colspan=\"9\" style=\"text-align:left;padding-left:10px;\">\r\n"
				+"<label class=\"searxh-content-label\" style=\"margin-right:10px;\"><input class=\"check_all\" type=\"checkbox\">全选</label>\r\n"
                + "<a class=\"btn-down-all operation\" data-optio=\"bg\" data-opt=\"download\" href=\"#\">下载所选</a>\r\n"
                + "<a class=\"btn-print-all operation\" data-optio=\"bg\" data-opt=\"print\" href=\"#\">打印所选</a>\r\n"
				+"</td>\r\n"
                +"</tr>\r\n";
        return result;
    }
    public string herf(string id,string ph, string ret,string remind)
    {
        DataSet dsmx = new DataSet();
        dsmx.Clear();
        string detailid = id;
        string retsult = "";
        string sql = "select rtrim(imageid) as imageid,rtrim(detailid)+replace(rtrim(fzxx1),' ','') as rel,rtrim(url) as url from [ImageDoc](nolock) where beactive='是' and detailid=rtrim('" + detailid + "') and replace(rtrim(fzxx1),' ','')=replace(rtrim('" + ph + "'),' ','')";
        dsmx = mssql.ExecuteDs(sql);
        if (dsmx.Tables[0].Rows.Count > 0)
        {
            retsult = retsult + "<!--" + sql + "-->";
            retsult +=  "<a class=\"btn-down operation\" href=\"#\" data-opt=\"download\" data-optio=\"bg\"  data-id=\"" + ret+"|"+ph + "\" data-batch=\"" + ph + "\">下载</a>\r\n";
            retsult += "<a class=\"btn-print operation\" href=\"#\" data-opt=\"print\" data-optio=\"bg\"  data-id=\"" + ret + "|" + ph + "\" data-batch=\"" + ph + "\">打印</a>\r\n";
            int rowid=0;
			for (int i = 0; i < dsmx.Tables[0].Rows.Count; i++)
            {
               string fileurl = Init.imagefileurl(dsmx.Tables[0].Rows[i]["url"].ToString());
                if (!string.IsNullOrEmpty(fileurl))
                {
                    retsult += " <a class=\"btn-view preview\" href=\"" + fileurl + "\" rel=\"" + dsmx.Tables[0].Rows[i]["rel"].ToString() + "\" " + (rowid == 0 ?"": " style=\"display:none;\"") + ">预览</a>\r\n";
					rowid++;
                }
                /*
                 if (i == 0)
                {
                    retsult += " <a class=\"btn-view preview\" href=\"" + dsmx.Tables[0].Rows[i]["url"].ToString() + "\" rel=\"" + dsmx.Tables[0].Rows[i]["rel"].ToString() + "\">预览</a>\r\n";
                }
                else
                {
                    retsult += " <a class=\"btn-view preview\" href=\"" + dsmx.Tables[0].Rows[i]["url"].ToString() + "\" rel=\"" + dsmx.Tables[0].Rows[i]["rel"].ToString() + "\" style=\"display:none;\">预览</a>\r\n";
                }
                 */
            }
        }
        else
        {

            retsult += " <a class=\"btn-tixing tixing\" href=\"#\" data-id=\"" + ret + "\" data-optio=\"bg\" data-batch=\"" + ph + "\">";
            retsult += remind.ToString() == "1" ? "<font color=\"#ff00\">已提醒</font>" : "提醒上传";
            retsult += "</a>\r\n";
        }
        return retsult;
    }
}